Where the values the whole game shares live in the Code tab, what each one starts as, and what happens when the game starts over.
A global is a value the whole game shares: a score, the lives left, whether a door is open. Any object can read it and change it, so it does not belong to one object.
In code you write it with global. in front:
global.score += 1;The Globals section is where each global is made, and where you say what it starts as. It is in the Code tab, directly under Macros. The in-editor Help Center topic covers the same ground from the buttons' side.
Click Globals in the list on the left of the Code tab. Each row is one global. It shows the name, what it starts as, and its kind in words: whole number, decimal, true / false or text. Click a row to change it.
| Button | What it does |
|---|---|
| + | Makes a new global: a name, a kind and what it starts as. |
| ✎ | Opens every global as text in a window, to type them directly. |
Right-click a row to change it, rename it, show it in the text, or delete it.
You choose the kind on purpose, because the game has to know how much room the value needs.
| Kind | Looks like | Notes |
|---|---|---|
| Whole number | 0, 25, -3 | No decimal point. It can never hold 1.5 later. |
| Decimal | 0.0, 1.5, -0.25 | Always has its decimal point. Code can give it whole numbers too. |
| True / false | true, false | Like whether a door is open. |
| Text | "Player One" | Words in quotes, on one line. It can be as long as the longest text a variable may hold (Longest string in Project Settings). Starts empty if you leave it blank. |
The trap to avoid: a global that starts as 0 is a whole number. If your code later says global.speed = 1.5;, Gbabel refuses it and tells you to change the kind. So if a value will ever have a fraction, pick Decimal from the start, and it starts as 0.0.
The kind is easy to change: open the global and pick another kind. The value is turned into the new kind's way of writing it (2.0 becomes 2).
Each global starts as the value in the section when the game starts, and again every time the whole game starts over.
game_restart() (a title screen's New game button does that), or switching the console off and on.room_restart() and room_change() are not a restart. A global keeps its value through a room change, which is the reason to use one.save_get), and that happens after the game starts, so the saved value is the one that stays:global.score = save_get("score", 0);Put that line in an object's Create event. The Globals section starts the score at 0, and this line replaces it with the saved score when there is one.
Only a plain number, true / false or text in quotes. A value that has to be worked out belongs in an event, where code can run. The section's text is one line for each global, and comments:
// What the game's shared values are when it starts.
global.score = 0;
global.speed = 1.5;
global.door_open = false;The globals are saved in one file at the top of your project folder, called globals.gbml. A project that never uses the section has no such file.
A name can use letters, numbers and _, and cannot start with a number. It cannot be a name the game already uses: an object, a script, a picture or sound, a named value in the Macros section, or something built in. The dialog tells you when a name is taken.
Rename changes the name everywhere it is used: in every event and script, in a room's Create code, in the code on a placed object, and in settings that name it (a HUD value that shows it, a cutscene answer that sets it). It is one step you can undo, and Gbabel tells you how many places changed. Only the name itself changes: words in comments and in "quotes" are left alone.
Delete says how many places use it first, and then lets you go ahead. The Problems list then shows each place that needs fixing. Undo brings it back.
Making, changing, renaming or deleting a global is one step you can undo with Ctrl+Z. The buttons save the file right away. Text you type in the window waits for Ctrl+S, like any code.
Writing global.score = 0; in an object's Create event still works, exactly as before, and a global that is only made that way is still a global. But if the same global also starts in the section, Gbabel shows a note pointing to both places, because the one in the Create event runs last and wins. Keep one: take out the line in the Create event, or take the global out of the section. A line that reads or works out a value, like global.score = save_get("score", 0);, is fine: that is what a Create event is for.
While the game runs, the Play tab's Inspector lists the globals as their own group, and you can change one there.
Only the Game Boy Advance writes the starting values so far. On another console the section is grayed out (or hidden, if you chose that in File > Preferences > Module) with a note, and a starting value written in code still works.
global.score = 0;. Move it into an object's event, where code can run.